package com.example.demo.util;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class GetDownLoadUrl {
    public static void main(String[] args) {
        ((LoggerContext) LoggerFactory.getILoggerFactory())
                .getLoggerList()
                .forEach(logger -> logger.setLevel(Level.ERROR));
//        getReplyIdList("D:\\input\\202309271653_3_input.txt","D:\\output\\202309271653_3_downloadurl.txt","D:\\output\\202309271653_3_sql.txt","live");
        getReplyIdList("D:\\input\\202309271653_1_5_input.txt","D:\\output\\202309271653_1_5_downloadurl.txt","D:\\output\\202309271653_1_5_sql.txt","live");
    }


    public static void getReplyIdList(String replyIdPath,String downLoadPath,String updateSqlPath,String type){
        List<String> replyIdList = new ArrayList<>();

        File file = new File(downLoadPath);
        FileWriter fw = null;
        BufferedWriter bw = null;

        File sqlFile = new File(updateSqlPath);
        FileWriter sqlFw = null;
        BufferedWriter sqlBw = null;

        try {
            //读取关键字
            FileInputStream inputStream = new FileInputStream(replyIdPath);
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));


            if(!file.exists()) {
                file.createNewFile();
            }

            if(!sqlFile.exists()) {
                sqlFile.createNewFile();
            }


            fw = new FileWriter( file.getAbsoluteFile(), true);  //true续写新内容
            bw = new BufferedWriter(fw);

            sqlFw = new FileWriter( sqlFile.getAbsoluteFile(), true);  //true续写新内容
            sqlBw = new BufferedWriter(sqlFw);


            String str = null;
            String insertStr = "";
            String downloadUrl = "";
            String updateStr = "";
            while((str = bufferedReader.readLine()) != null)
            {
                String responseStr = HttpClientUtils.toPost(new HashMap(),str);
                if(!StringUtils.isEmpty(responseStr)){
                    JSONObject jsonObject =  JSON.parseObject(responseStr);
                    JSONObject dataMap = (JSONObject) jsonObject.get("data");
                    if(dataMap==null){
                        continue;
                    }else{
                        updateStr = "update live_replay_record set replay_video_record='";
                        if(type.equals("medic")){
                            updateStr = "update medic_live_replay_manage_record set  reply_vedio_url='";
                        }
                        downloadUrl = dataMap.get("downloadUrl").toString();
                        if(StringUtils.isEmpty(downloadUrl)){
                            System.out.println("replay_id:"+str+"获取的下载地址为空！");
                            continue;
                        }
                        updateStr += downloadUrl;
                        if(type.equals("medic")){
                            updateStr += "' where reply_id='"+str+"';";
                        }else{
                            updateStr += "' where replay_id_record='"+str+"';";
                        }
                    }
//                    String downloadUrl = dataMap==null?"无":dataMap.get("downloadUrl").toString();
//                    insertStr = downloadUrl +" " + str;
                }else{
                    continue;
                }
                bw.write(downloadUrl+"\r\n");
                sqlBw.write(updateStr+"\r\n");

            }

            bw.close();
            sqlBw.close();
            //close
            inputStream.close();
            bufferedReader.close();
        }catch (IOException io) {
            io.printStackTrace();
        }
    }
}
